﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HotelManager.Business_Logic_Layer;
using HotelManager.Data_Transfer_Layer;
using HotelManager.GUI;

namespace HotelManager
{
    public partial class QuanLyPhong : UserControl
    {
        khuvucBLO kv_blo;
        loaiphongBLO lp_blo;
        phongBLO p_blo;
        string phong = "";
        int luachon = -1;
        public string truyendulieuphong
        {
            set { phong = value; }
        }
        public int truyendulieuluachon
        {
            set { luachon = value; }
        }

        public QuanLyPhong()
        {
            InitializeComponent();
            pnlContent.AutoScroll = true;
            kv_blo = new khuvucBLO();
            lp_blo = new loaiphongBLO();
            p_blo = new phongBLO();
            Load_Data();
            Load_dsphong();
        }
        public void demo()
        {
            pnlContent.Controls.Clear();
            Load_dsphong();
        }

        public void Load_Data()
        {
            cbbKhuvuc.DataSource = kv_blo.get_arr();
        }

        public int get_RadioValue()
        {
            string lp_tenloaiphong = "";
            int lp_id = -1;
            if (radStandard.Checked == true)
            {
                lp_tenloaiphong = radStandard.Text;
                loaiphongDTO lp_dto = new loaiphongDTO();
                lp_dto = lp_blo.View_DetailbyName(lp_tenloaiphong);
                lp_id = lp_dto.Lp_id;
            }
            else
            {
                if (radSuperior.Checked == true)
                {
                    lp_tenloaiphong = radSuperior.Text;
                    loaiphongDTO lp_dto = new loaiphongDTO();
                    lp_dto = lp_blo.View_DetailbyName(lp_tenloaiphong);
                    lp_id = lp_dto.Lp_id;
                }
                else
                {
                    if (radDeluxe.Checked == true)
                    {
                        lp_tenloaiphong = radDeluxe.Text;
                        loaiphongDTO lp_dto = new loaiphongDTO();
                        lp_dto = lp_blo.View_DetailbyName(lp_tenloaiphong);
                        lp_id = lp_dto.Lp_id;
                    }
                }
            }
            return lp_id;
        }

        public int get_ComboboxValue()
        {
            string kv_tenkhuvuc = cbbKhuvuc.Text; 
            int kv_id = -1;
            if (kv_tenkhuvuc != "Tất cả")
            {
                khuvucDTO kv_dto = new khuvucDTO();
                kv_dto = kv_blo.View_DetailbyName(kv_tenkhuvuc);
                kv_id = kv_dto.Kv_id;
            }
            return kv_id;
        }

        public Button init_Button(string name, string status, int X, int Y)
        {
            Button button = new Button();
            button.Name = "btn" + name;
            button.Text = name;
            Point size = new Point(60, 50);
            button.Size = new Size(size);
            if (status == "Trống")
                button.BackColor = Color.White;
            else
            {
                if (status == "Đang ở")
                    button.BackColor = Color.Olive;
                else
                {
                    button.BackColor = Color.Green;
                }
            }            
            button.Location = new Point(X, Y);
            button.ForeColor = Color.Maroon;
            button.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Bold);
            button.Click += new EventHandler(btn_Click);
            return button;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            MouseEventArgs me = (MouseEventArgs)e;
            Button btn = (Button)sender;
            string msg="";
            if (btn.BackColor == Color.White)
            {
               // msg = "Phòng trống";
                NhanPhong frm_nhanphong = new NhanPhong();
                frm_nhanphong.truyendulieu = btn.Text;
                frm_nhanphong.Show();
            }
            else
            {
                if (btn.BackColor == Color.Olive)
                {
                    //msg = "Đang ở";
                    ThanhToanPhong frm_thanhtoan = new ThanhToanPhong();
                    frm_thanhtoan.truyendulieuphong = btn.Text;
                    frm_thanhtoan.Show();
                }
                else
                {
                    //msg = "Đã đặt";
                    NhanPhong frm_nhanphong = new NhanPhong();
                    frm_nhanphong.truyendulieu = btn.Text;
                    frm_nhanphong.Show();
                }
            }
            
            //MessageBox.Show(msg);
        }

        public void Load_dsphong()
        {
            pnlContent.Controls.Clear();
            int kv_id = get_ComboboxValue();
            int lp_id = get_RadioValue();
            //**************************************
            int n = p_blo.List_current(kv_id,lp_id).Count();
            phongDTO[] list_phong = new phongDTO[n];
            list_phong = p_blo.List_current(kv_id, lp_id);
            Button[] btn = new Button[n];
            int i = 0, W = 60, H = 50, X = 30, Y = 5, n_phongtrong = 0, n_dango = 0, n_dadat = 0;
            foreach (phongDTO obj in list_phong)
            {
                int p_id = obj.P_id;
                btn[i] = new Button();
                btn[i] = init_Button(obj.P_tenphong,obj.P_tinhtrang, X, Y);
                string tinhtrang = obj.P_tinhtrang;
                if (tinhtrang == "Trống")
                    n_phongtrong++;
                else
                {
                    if (tinhtrang == "Đang ở")
                        n_dango++;
                    else
                        n_dadat++;
                }
                X = (X + W + 20);
                pnlContent.Controls.Add(btn[i]);
                i++;
            }
            txtDadat.Text = n_dadat.ToString();
            txtDango.Text = n_dango.ToString();
            txtTrong.Text = n_phongtrong.ToString();
            //MessageBox.Show(kv_id.ToString() + "-" + lp_id.ToString());
        }

        private void cbbKhuvuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(cbbKhuvuc.Text);
            Load_dsphong();

        }

        private void radStandard_CheckedChanged(object sender, EventArgs e)
        {
            if (radStandard.Checked == true)
            {
                Load_dsphong();
            }
        }

        private void radSuperior_CheckedChanged(object sender, EventArgs e)
        {
            if (radSuperior.Checked == true)
            {
                Load_dsphong();
            }
        }

        private void radDeluxe_CheckedChanged(object sender, EventArgs e)
        {
            if (radDeluxe.Checked == true)
            {
                Load_dsphong();
            }
        }

        private void radAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radAll.Checked == true)
            {
                Load_dsphong();
            }
        }
    }
}
